<?php

namespace app\business\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Advice extends Controller
{
    public function addAdvice()
    {
        $usId = trim(input('param.usId'));
        $tenantId = trim(input('param.tenantId'));
        $suggestType = trim(input('param.suggestType'));
        $content = trim(input('param.content'));
        $imgs = input('param.imgs');
        $len=count($imgs);
        $this->db = new SysDb();
        $imglist=array();
        for($i=0;$i<$len;$i++){
        $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';

        if (strstr($imgs[$i], ",")) {
            $image = explode(',', $imgs[$i]);
            $image = $image[1];
        }
        $path = "./uploads/" . date("Ymd", time());
        $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        $imageSrc = $path . "/" . $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
        if (!$r) {
            exit(json_encode(['message' => 'error', 'result' => "失败"]));
           
        }
        $imageSrcs = $paths . "/" . $imageName; //图片名字
        array_push($imglist,$imageSrcs);
    }
             $str = implode(',',$imglist);
             $data['tenant_id']=$tenantId;
             $data['bus_id']= $usId;
             $data['suggest_type']=$suggestType;
             $data['content']=$content;
             $data['type']=1;
             $data['imgs']=$str;
             $data['reply']="";
             $data['remark']="";
             $data['status']=0;
             $this->db = new SysDb();
             $this->db->table('sys_advice')->insert($data);
            exit(json_encode(['message' => 'succ', 'result' =>"添加成功"]));
          
        
    }
    public function addAdvicelist(){
        $id = trim(input('param.id'));
        $page = trim(input('param.pageNo'));
        $limit= trim(input('param.pageSize'));
  
       
        // $count = $this->db->table('newslist')->count();
        $tol = (intval($page) - 1) * $limit;
        // ->where('type', $type)
       
           $list =DB::name("sys_advice")->order("create_time desc")->where('bus_id', $id)->limit($tol, $limit)->select();
        // $list=$this->db->table('order_info')->where(['bus_id' =>$id])->lists();
        $this->db = new SysDb();
        $count = $this->db->table('sys_advice')->where(['bus_id' => $id]) ->count();
        $data=array();
        array_push($data,$list);
        array_push($data,$count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));

    }
}
